package com.yan.controller;


import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.kisso.Token;
import com.baomidou.kisso.common.IpHelper;
import com.baomidou.kisso.common.util.HttpUtil;
import com.baomidou.kisso.web.waf.request.WafRequestWrapper;
import com.jfinal.core.Controller;
import com.yan.annon.RouterMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * Created by Administrator on 2016/12/26 0026.
 */
//登陆不用拦截
@RouterMapping(url = "/sso", viewPath = "/")
public class TestSSOLoginController extends Controller {

    private static final Logger log = LoggerFactory.getLogger(TestSSOLoginController.class);

    public void index() {
        Token token = SSOHelper.getToken(getRequest());

        //令牌不为空,让用户直接访问主页,免登陆.
        if (token != null) {
            redirect("/");
            return;
        }
        //判断是否是POST提交
        if (HttpUtil.isPost(getRequest())) {
            //包装request请求
            WafRequestWrapper wafRequestWrapper = new WafRequestWrapper(getRequest());
            String name = wafRequestWrapper.getParameter("name");
            String password = wafRequestWrapper.getParameter("password");
            //验证账号密码,此处暂时不用去数据库校验
            if ("test".equals(name) && "test".equals(password)) {
                token = new SSOToken();
                token.setUid(UUID.randomUUID().toString());
                token.setIp(IpHelper.getIpAddr(getRequest()));
                token.setId(UUID.randomUUID().toString());
                //保存token立即销毁信任的JSESSIONID
                SSOHelper.setSSOCookie(getRequest(), getResponse(), token, true);
                redirect("/index.html");
                return;
            }


        }

        render("/login.jsp");

    }


}